/*
 * @Description:
 * @Author: william
 * @Date: 2025-06-03 09:21:17
 * @LastEditors: william
 * @LastEditTime: 2025-07-05 13:45:15
 */
// src/store/app.js
import { setCookieLang } from '@/utils/cookies'

export const useAppStore = defineStore(
  'app',
  () => {
    const versionName = ref('')
    const language = ref(g_i18n.global.locale.value)
    const systemInfo = shallowRef({})

    systemInfo.value = uni.getSystemInfoSync()
    versionName.value = systemInfo.value.appWgtVersion || systemInfo.value.appVersion

    const setLanguage = lang => {
      language.value = lang
      setCookieLang(lang)
    }

    const upLoadAppClientInfo = async (userInfo) => {
      if (!systemInfo.value?.deviceId) {
        throw new Error('获取系统信息失败')
      }
      await g_api.client.apiUploadAppClientInfo({ 
        appId: systemInfo.value.deviceId,
        mobileOs: systemInfo.value.osName,
        mobileOsVersion: systemInfo.value.osVersion,
        mobileManufacture: systemInfo.value.deviceBrand,
        loginUserId: userInfo.userId,
        loginUserName: userInfo.nickName,
        tenantId: userInfo.tenantId,

      })
    }

    return {
      versionName,
      language,
      setLanguage,
      upLoadAppClientInfo
    }
  }
  // {
  //   persist: true // 配置持久化
  // }
)

export function useOutsideAppStore() {
  return useAppStore(g_store)
}
